package com.xxx.protobuf.server;

import com.xxx.protobuf.MessageProto;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * protobuf： server端handler
 */
public class ProtobufServerHandler extends SimpleChannelInboundHandler<MessageProto.Request> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MessageProto.Request request) {
        System.out.println("Received from client request: " + request.getQuery());
        
        // 构建响应
        MessageProto.Response response = MessageProto.Response.newBuilder()
                .addResult("Result 1 for " + request.getQuery())
                .addResult("Result 2 for " + request.getQuery())
                .setTotalPages(request.getPageNumber() + 1)
                .build();
        
        ctx.writeAndFlush(response);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}